\defmodule {TableFormat}

This class provides methods to format arrays and matrices into
\class{String}s in different styles.
This  could be useful for printing arrays and subarrays, or for
putting them in files for further treatment by other softwares such
as {\em Mathematica\/}, {\em Matlab\/}, etc.

\bigskip\hrule

\begin{code}\begin{hide}
/*
 * Class:        TableFormat
 * Description:  Provides methods to format arrays into String's
 * Environment:  Java
 * Software:     SSJ
 * Copyright (C) 2001  Pierre L'Ecuyer and Universite de Montreal
 * Organization: DIRO, Universite de Montreal
 * @author
 * @since

 * SSJ is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License (GPL) as published by the
 * Free Software Foundation, either version 3 of the License, or
 * any later version.

 * SSJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * A copy of the GNU General Public License is available at
   <a href="http://www.gnu.org/licenses">GPL licence site</a>.
 */
\end{hide}
package umontreal.iro.lecuyer.util;

public class TableFormat\begin{hide} {
   private TableFormat() {}\end{hide}
\end{code}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection*{Formating styles}
\begin{code}

   public static final int PLAIN\begin{hide} = 0; \end{hide}
\end{code}
\begin{tabb}   Plain text matrix printing style
\end{tabb}
\begin{code}

   public static final int MATHEMATICA\begin{hide} = 1; \end{hide}
\end{code}
\begin{tabb}   Mathematica matrix printing style
\end{tabb}
\begin{code}

   public static final int MATLAB\begin{hide} = 2; \end{hide}
\end{code}
  \begin{tabb} Matlab matrix printing style
  \end{tabb}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection*{Functions to convert arrays to \texttt{String}}
\begin{code}

   public static String format (int V[], int n1, int n2, int k, int p)\begin{hide} {
      int i;
      StringBuffer sb = new StringBuffer();
      if (k > 1) {
         sb.append ("Elements  " + n1 + "  to  " + n2 +
                     PrintfFormat.NEWLINE + PrintfFormat.NEWLINE);
         for (i = n1; i <= n2; i++) {
            sb.append (PrintfFormat.d (p, V[i]));
            if (((i + 1 - n1) % k) == 0)
               sb.append (PrintfFormat.NEWLINE);
         }
         sb.append (PrintfFormat.NEWLINE);
      }
      else {
         sb.append (PrintfFormat.NEWLINE + " Index        Element" +
                    PrintfFormat.NEWLINE);
         for (i = n1; i <= n2; i++)
            sb.append (PrintfFormat.d (6, i) + "   " +
                      PrintfFormat.d (12, V[i]) + PrintfFormat.NEWLINE);
      }
      sb.append (PrintfFormat.NEWLINE);
      return sb.toString();
   }\end{hide}
\end{code}
 \begin{tabb}  Formats a \class{String} containing the elements \texttt{n1}
  to \texttt{n2} (inclusive) of table \texttt{V},
  \texttt{k} elements per line, \texttt{p} positions per element.
  If  \texttt{k} = 1, the array index will also appear on the left
  of each element, i.e., each line \texttt{i} will have the form \texttt{i V[i]}.
 \end{tabb}
\begin{htmlonly}
   \param{V}{array to be formated}
   \param{n1}{index of the first element being formated}
   \param{n2}{index of the last element being formated}
   \param{k}{number of elements per line}
   \param{p}{number of positions per element}
   \return{formated string repreenting the elements}
\end{htmlonly}
\begin{code}

   public static String format (double V[], int n1, int n2,
                                int k, int p1, int p2, int p3)\begin{hide} {
      int i;
      StringBuffer sb = new StringBuffer();
      if (k > 1) {
         sb.append ("Elements  " + n1 + "  to  " + n2  +
                     PrintfFormat.NEWLINE + PrintfFormat.NEWLINE);
         for (i = n1; i <= n2; i++) {
            sb.append (PrintfFormat.format (p1, p2, p3, V[i]));
            if (((i + 1 - n1) % k) == 0)
               sb.append (PrintfFormat.NEWLINE);
         }
         sb.append (PrintfFormat.NEWLINE);

      } else {
         sb.append (PrintfFormat.NEWLINE + " Index            Element" +
                    PrintfFormat.NEWLINE);
         for (i = n1; i <= n2; i++)
            sb.append (PrintfFormat.d (6, i) + "   " +
                       PrintfFormat.format (p1, p2, p3, V[i]) +
                       PrintfFormat.NEWLINE);
      }
      sb.append (PrintfFormat.NEWLINE);
      return sb.toString();
   }\end{hide}
\end{code}
 \begin{tabb}  Similar to the previous method, but for an array of \texttt{double}'s.
  Gives at least \texttt{p1} positions per element,
  \texttt{p2} digits after the decimal point, and at least \texttt{p3}
  significant digits.
 \end{tabb}
\begin{htmlonly}
   \param{V}{array to be formated}
   \param{n1}{index of the first element being formated}
   \param{n2}{index of the last element being formated}
   \param{k}{number of elements per line}
   \param{p1}{number of positions per element}
   \param{p2}{number of digits after the decimal point}
   \param{p3}{number of significant digits}
   \return{formated string repreenting the elements}
\end{htmlonly}
\begin{code}
\begin{hide}
   private static int Style = PLAIN;

   private static char OuvrantMat = ' ';     // Matrix delimitors
   private static char FermantMat = ' ';

   private static char OuvrantVec = ' ';     // Vector delimitors
   private static char FermantVec = ' ';

   private static char SepareVec = ' ';      // Element separators
   private static char SepareElem = ' ';

   private static void fixeDelim (int style) {
      /* Fixe les delimiteurs pour imprimer une matrice selon un format
         approprie */
      Style = style;
      switch (style) {
      case MATHEMATICA:
         OuvrantMat = '{';
         FermantMat = '}';
         OuvrantVec = '{';
         FermantVec = '}';
         SepareVec = ',';
         SepareElem = ',';
         break;
      case MATLAB:
         OuvrantMat = '[';
         FermantMat = ']';
         OuvrantVec = ' ';
         FermantVec = ' ';
         SepareVec = ' ';
         SepareElem = ' ';
         break;
      default:
         OuvrantMat = ' ';
         FermantMat = ' ';
         OuvrantVec = ' ';
         FermantVec = ' ';
         SepareVec = ' ';
         SepareElem = ' ';
         break;
      }
   }


   @Deprecated
   public static String format (int[][] Mat, int i1, int i2,
                                int j1, int j2, int w, int p,
                                int style, String Name) {
      return format (Mat, i1, i2, j1, j2, w, style, Name);
   }\end{hide}

   public static String format (double[][] Mat, int i1, int i2,
                                int j1, int j2, int w, int p,
                                int style, String Name)\begin{hide} {
      int k;
      int j;
      int i;
      double x;
      String S;

      fixeDelim (style);
      StringBuffer sb = new StringBuffer();
      if (Name.length() > 0)
         sb.append (Name + " = ");

      double prec = Math.pow (10.0, (double)p);
      sb.append (OuvrantMat + PrintfFormat.NEWLINE);
      for (i = i1; i <= i2; i++) {
         sb.append (OuvrantVec);
         for (j = j1; j <= j2; j++) {
            sb.append (' ');
            switch (style) {
            case MATHEMATICA:
               x = Mat[i][j];
               if (((x != 0.0) && (Math.abs (x) < 0.1)) ||
                   (Math.abs (x) > prec)) {
                  S = PrintfFormat.G (0, p, x);
                  int exppos = S.indexOf ('E');
                  if (exppos != -1)
                     S = S.substring (0, exppos) + "*10^(" +
                          S.substring (exppos+1) + ")";
               }
               else
                  S = PrintfFormat.f (0, p, x);
               S = PrintfFormat.s (w, S);
               break;
            default:
               // MATLAB, Default */
               sb.append (PrintfFormat.G (w, p, Mat[i][j]));
               break;
            }
            if (j < j2)
               sb.append (SepareElem);
         }
         sb.append (FermantVec);
         if (i < i2)
            sb.append (SepareVec + PrintfFormat.NEWLINE);
      }
      sb.append (FermantMat + PrintfFormat.NEWLINE);
      return sb.toString();
   }\end{hide}
\end{code}
 \begin{tabb} Formats the submatrix with lines
   \texttt{i1} $\le i \le $ \texttt{i2} and columns
   \texttt{j1} $\le j \le $ \texttt{j2} of the matrix \texttt{Mat}, using the
   formatting style \texttt{style}. The elements are formated in \texttt{w}
   positions each, with a precision of \texttt{p} digits.
   \texttt{Name} provides an identifier for the submatrix.
%
   To be treated by \texttt{Matlab}, the returned string
   must be copied to a file with extension \texttt{.m}.
   If the file is named \texttt{poil.m}, for example, it can be accessed by
   calling \texttt{poil} in \texttt{Matlab}.
   For \texttt{Mathematica}, if the file is named \texttt{poil},
   it will be read using \texttt{<< poil;}.
 \end{tabb}
\begin{htmlonly}
   \param{Mat}{matrix to be formated}
   \param{i1}{index of the first row being formated}
   \param{i2}{index of the last row being formated}
   \param{j1}{index of the first column being formated}
   \param{j2}{index of the last column being formated}
   \param{w}{number of positions for each element}
   \param{p}{number of digits after the decimal point of the elements}
   \param{style}{formating style of the submatrix, being one of
        \method{MATHEMATICA}{}, \method{MATLAB}{}, or \method{PLAIN}{}}
   \param{Name}{descriptive name of the submatrix}
   \return{formated string representing the submatrix}
\end{htmlonly}
\begin{code}

   public static String format (int[][] Mat, int i1, int i2, int j1, int j2,
                                int w, int style, String Name)\begin{hide} {
      int i;
      int j;

      fixeDelim (style);
      StringBuffer sb = new StringBuffer();
      if (Name.length() > 0)
         sb.append (Name + " = ");

      sb.append (OuvrantMat + PrintfFormat.NEWLINE);
      for (i = i1; i <= i2; i++) {
         sb.append (OuvrantVec);
         for (j = j1; j <= j2; j++) {
            sb.append (PrintfFormat.d (w, Mat[i][j]));
            if (j < j2)
               sb.append (SepareElem);
         }
         sb.append (FermantVec);
         if (i < i2)
            sb.append (SepareVec + PrintfFormat.NEWLINE);
      }
      sb.append (FermantMat + PrintfFormat.NEWLINE + PrintfFormat.NEWLINE);
      return sb.toString();
   }
 }\end{hide}
\end{code}
\begin{tabb} Similar to the previous method, but for a matrix of \texttt{int}'s.
\end{tabb}
\begin{htmlonly}
   \param{Mat}{matrix to be formated}
   \param{i1}{index of the first row being formated}
   \param{i2}{index of the last row being formated}
   \param{j1}{index of the first column being formated}
   \param{j2}{index of the last column being formated}
   \param{w}{number of positions for each element}
   \param{style}{formating style of the submatrix, being one of
        \method{MATHEMATICA}{}, \method{MATLAB}{}, or \method{PLAIN}{}}
   \param{Name}{descriptive name of the submatrix}
   \return{formated string representing the submatrix}
\end{htmlonly}
